home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Code Resources / Jims CDEFs 1.50 / demo Source ƒ / demoDialog.c < prev    next >
Encoding:
Text File  |  1995-10-27  |  33.7 KB  |  1,051 lines  |  [TEXT/KAHL]

  1. // -----------------------------------------------------------------------------
  2. //    File        : demoDialog.c
  3. //    Date        : August 24, 1994
  4. //    Author        : Jim Stout
  5. //    Purpose        : A demonstration routine for :
  6. //                    - my collection of CDEF's
  7. //                    - dialogAssist.c dialog utilities
  8. //                    - panelAssist.c tabPanel CDEF handling
  9. //                    - movableModal.c dialog routines
  10. //
  11. //    Note the special usage of dim text routines required when using
  12. //    AppendDITL and ShortenDITL routines.  See file dimText.c.
  13. //
  14. //    This demo uses the "TabPanel" CDEF - see "About CDEF's…".
  15. //
  16. // -----------------------------------------------------------------------------
  17. #include <GestaltEqu.h>
  18. #include <stdio.h>
  19.  
  20. // demo routines
  21. #include "demoWind.h"
  22. #include "demoDialog.h"
  23.  
  24. // utility routines
  25.  
  26. #include "dialogAssist.h"
  27. #include "dimText.h"
  28. #include "panelAssist.h"
  29. #include "movableModal.h"
  30. #include "TogLib.h"
  31.  
  32. // cdef #defines
  33.  
  34. #include "jimsCDEF.h"
  35.  
  36. //=============================================================================
  37. // number of controls on each panel
  38. //=============================================================================
  39. #define BUTTONCNT    14
  40. #define POPUPCNT    6
  41. #define SPINNERCNT    8
  42. #define DATECNT        6
  43. #define HSLIDERCNT    10
  44. #define VSLIDERCNT    6
  45. #define PROGBARCNT    9
  46. #define POPEXTCNT    9
  47.  
  48. //=============================================================================
  49. // demoDialog globals - temporary storage for control values
  50. //=============================================================================
  51.  
  52. short gFontNum, gFontSize;
  53. short gButtonValues[BUTTONCNT];
  54. short gPopUpValues[POPUPCNT];
  55. short gSpinnerValues[SPINNERCNT];
  56. long  gDateValues[DATECNT];
  57. short gHSliderValues[HSLIDERCNT];
  58. short gVSliderValues[VSLIDERCNT];
  59. short gProgressValues[PROGBARCNT];
  60. short gPopExtensions[POPEXTCNT];
  61.  
  62. ControlActionUPP gTrackSlider,gTrackSpin,gFractSpin;
  63.  
  64. extern SysEnvRec        gSysEnv;
  65. //=============================================================================
  66. // demo the tab dialog panel
  67. //=============================================================================
  68. extern void demoDialog()
  69. {
  70.  
  71.     DialogPtr        theDialog;
  72.     ModalFilterUPP    filterUPP;
  73.     GrafPtr            savePort;
  74.     short            itemHit, inx, currPanel=0,toPanel;
  75.     Boolean            dimIt = true;
  76.     
  77. //-----------------------------------------------------------------------------
  78. // Make sure we have the DITL extensions we need for the PanelSwap()
  79. // routine.  These extensions are present in System 7 but require the
  80. // CommToolbox under System 6.
  81. //
  82. // If you want to use the DITL extensions under System 6, make sure
  83. // you include the CommToolbox libraries and call InitCRM() and
  84. // InitCTBUtilities() after initializing the toolbox.
  85. //-----------------------------------------------------------------------------
  86.  
  87.     if(daGestalt(gestaltDITLExtAttr) == -1L) {
  88.         if(daGestalt(gestaltCTBVersion) < 0x0100) {
  89.             StopAlert(256, nil);
  90.             return;
  91.         }
  92.     }
  93.  
  94. //-----------------------------------------------------------------------------
  95. // Create our UniversalProcPtr's
  96. //-----------------------------------------------------------------------------
  97.     filterUPP = NewModalFilterProc(filter);
  98.     if(filterUPP == nil)                            // a "real" program would
  99.         return;                                        // probably check these too..
  100.         
  101.     gTrackSlider = NewControlActionProc(trackSlider);
  102.     gTrackSpin = NewControlActionProc(trackSpin);
  103.     gFractSpin = NewControlActionProc(fractSpin);
  104.     
  105. //-----------------------------------------------------------------------------
  106. //    Create the dialog
  107. //-----------------------------------------------------------------------------
  108.     theDialog = GetNewDialog(128,0L,(DialogPtr)-1);
  109.     if(theDialog) {
  110.         GetPort(&savePort);
  111.         SetPort(theDialog);    
  112.         
  113.         if(!gSysEnv.hasColorQD)
  114.             HideControl(daGetCtlHandle(theDialog, BGCOLOR));
  115.     
  116.         initDimText(theDialog);                    // set up for dimmable text
  117.         
  118. //-----------------------------------------------------------------------------
  119. // initialize the dialog to the correct panel
  120. //-----------------------------------------------------------------------------
  121.  
  122.         toPanel = daGetCtlValue(theDialog, TABCNTL);
  123.         
  124.         disposeDimData(theDialog);
  125.         if(!panelSwap(theDialog, 128, currPanel, toPanel, STDCTLS))
  126.             goto cleanUp;
  127.         makeDimmable(theDialog);
  128.         
  129. //-----------------------------------------------------------------------------
  130. // a "real" application would probably get control settings from some sort
  131. // of configuration file/record/structure.  We'll just initialize our stuff
  132. // by calling savePanel() which will take the values from the CNTL templates.
  133. //-----------------------------------------------------------------------------
  134.  
  135.         savePanel(theDialog, toPanel);
  136.         
  137. //-----------------------------------------------------------------------------
  138. // need to to find "Chicago" in the font popup, or we will start out with the
  139. // first font in the popup.  Want to start with the standard system font.
  140. //-----------------------------------------------------------------------------
  141.  
  142.         if(toPanel == BUTTONS) {
  143.             gFontNum = systemFont;
  144.             gFontSize = 0;
  145.             gButtonValues[12] = 12;
  146.             gButtonValues[13] = findSystemFont(theDialog);
  147.         }
  148.         
  149. //-----------------------------------------------------------------------------
  150. // restore control settings and initialize the panel.
  151. //-----------------------------------------------------------------------------
  152.  
  153.         restorePanel(theDialog, toPanel);
  154.         currPanel = toPanel;
  155.         
  156. //-----------------------------------------------------------------------------
  157. // show the dialog and start handling events
  158. //-----------------------------------------------------------------------------
  159.  
  160.         ShowWindow(theDialog);
  161.         
  162.         do {
  163.         
  164. //            ModalDialog    (filterUPP, &itemHit);
  165.             movableModalDialog    (filterUPP,&itemHit);
  166.             
  167. //-----------------------------------------------------------------------------
  168. // a click on a tab, change to that panel
  169. //-----------------------------------------------------------------------------
  170.  
  171.             if(itemHit == TABCNTL) {
  172.             
  173.                 toPanel = daGetCtlValue(theDialog, TABCNTL);
  174.                 
  175.                 if(toPanel != currPanel) {
  176.                 
  177. //-----------------------------------------------------------------------------
  178. // save settings of current panel first
  179. //-----------------------------------------------------------------------------
  180.         
  181.                     savePanel(theDialog, currPanel);
  182.                 
  183. //-----------------------------------------------------------------------------
  184. // switch to the new panel and restore/initialize it.  Since we are using
  185. // the dimText.c routines with AppendDITL, dispose of the dim data stuff.
  186. //-----------------------------------------------------------------------------
  187.  
  188.                     disposeDimData(theDialog);
  189.                     if(!panelSwap(theDialog, 128, currPanel, toPanel, STDCTLS))
  190.                         goto cleanUp;
  191.                     makeDimmable(theDialog);
  192.  
  193.                     restorePanel(theDialog, toPanel);
  194.         
  195.                     currPanel = toPanel;
  196.                 
  197. //-----------------------------------------------------------------------------
  198. // re-enable the Disable/Enable button if needed
  199. //-----------------------------------------------------------------------------
  200.  
  201.                     if(!dimIt) {
  202.                         daDimOne(theDialog, DISABLE, false);
  203.                         daSetCtlTitle(theDialog, DISABLE, "\pDisable");
  204.                         dimIt = true;
  205.                     }
  206.                 }
  207.             }
  208.             else
  209.  
  210. //-----------------------------------------------------------------------------
  211. // show what the disabled controls look like
  212. //-----------------------------------------------------------------------------
  213.  
  214.             if(itemHit == DISABLE) {
  215.                 if(dimIt)
  216.                     daSetCtlTitle(theDialog, DISABLE, "\pEnable");
  217.                 else
  218.                     daSetCtlTitle(theDialog, DISABLE, "\pDisable");
  219.                 inx = CountDITL(theDialog);
  220.                 daDimItems(theDialog, STDCTLS+1, inx, dimIt);
  221.                 dimIt = !dimIt;
  222.                 if(currPanel == BUTTONS)
  223.                     daDimOne(theDialog, PB1, true);
  224.             }
  225.             else
  226.             if(itemHit == BGCOLOR)
  227.                 changeBGColor(theDialog);
  228.             else
  229.             if(itemHit == BGPAT)
  230.                 changeBGPat(theDialog, true);
  231.             
  232. //-----------------------------------------------------------------------------
  233. // handle a click on some other control within the panel
  234. //-----------------------------------------------------------------------------
  235.  
  236.                 processPanel(theDialog, currPanel, itemHit);
  237.                 
  238.         }while(itemHit != ok && itemHit != cancel);
  239.     
  240.         if(itemHit == ok) {
  241. //            savePanel(theDialog, currPanel);
  242.  
  243. //-----------------------------------------------------------------------------
  244. //    some code to apply the temporary storage control values
  245. //    in your application could go here.
  246. //-----------------------------------------------------------------------------
  247.  
  248.         }
  249. cleanUp:
  250.         disposeDimText(theDialog);
  251.         DisposDialog(theDialog);
  252.         DisposeRoutineDescriptor(gTrackSlider);
  253.         DisposeRoutineDescriptor(gTrackSpin);
  254.         DisposeRoutineDescriptor(gFractSpin);
  255.         DisposeRoutineDescriptor(filterUPP);
  256.     
  257.         SetPort(savePort);
  258.     }
  259. }
  260.  
  261. //=============================================================================
  262. // Handle a click on a control
  263. //=============================================================================
  264. static void processPanel(DialogPtr theDialog, short currPanel, short itemHit)
  265. {
  266.     ControlHandle        h;
  267.     short                t;
  268.     Rect                r;
  269.     long                num, newSize;
  270.     Str255                fontSize;
  271.     unsigned long        secs;
  272.     popUpPrivateDataH    hPpd;
  273.     
  274.     switch (currPanel) {
  275.         case BUTTONS:
  276.             switch(itemHit) {
  277.                 case CB1:                // click on a checkBox
  278.                 case CB2:
  279.                 case CB3:
  280.                 case CB4:
  281.                     daToggleCheck(theDialog, itemHit);
  282.                     resetTogButtons(theDialog,TB1);
  283.                 break;
  284.                 case RB1:                // click on a radioButton
  285.                 case RB2:
  286.                 case RB3:
  287.                 case RB4:
  288.                     daToggleRadio(theDialog, itemHit, RB1, RB4);
  289.                     resetTogButtons(theDialog,TB1);
  290.                 break;
  291.                 case TB1:                // click on a TogButton
  292.                 case TB2:
  293.                 case TB3:
  294.                 case TB4:
  295.                     setTogButtons(theDialog, itemHit, TB1, TB4);
  296.                 break;
  297.                 case EDITSIZE:            // keydown in edit text
  298.                     num = daGetINum(theDialog, EDITSIZE);
  299.                     adjustSizePopup(theDialog, num);
  300.                     resetTogButtons(theDialog,TB1);
  301.                 break;
  302.                 case SIZEPOP:
  303.                     GetDItem(theDialog,SIZEPOP,&t,(Handle *)&h,&r);
  304.                     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  305.                     
  306.                     newSize = GetCtlValue(h);
  307.                     GetItem((*hPpd)->mHandle, newSize, fontSize);
  308.                     daSetIText(theDialog, EDITSIZE, fontSize);
  309.                     daSelIText(theDialog, EDITSIZE);
  310.                     resetTogButtons(theDialog,TB1);
  311.                 break;
  312.                 case SETFONT:
  313.                     setNewFont(theDialog);
  314.                     resetTogButtons(theDialog,TB1);
  315.                 break;
  316.             }
  317.         break;
  318.  
  319. //-----------------------------------------------------------------------------
  320. // set the value of the arrow control to match the edit text item
  321. //-----------------------------------------------------------------------------
  322.  
  323.         case SPINNERS:
  324.             if(itemHit == EDITSPIN) {
  325.                 num = daGetINum(theDialog, EDITSPIN);
  326.                 daSetCtlValue(theDialog, SPIN3, (short)num);
  327.             }
  328.         break;
  329.         
  330. //-----------------------------------------------------------------------------
  331. // reset the date time controls if RESET button clicked
  332. //-----------------------------------------------------------------------------
  333.  
  334.         case DATETIME:
  335.             if(itemHit == RESET) {
  336.                 GetDateTime(&secs);                        // new date/time
  337.                 for(t=1;t<=6;t++) {
  338.                     h = daGetCtlHandle(theDialog, t+STDCTLS);
  339.                     if(h) {
  340.                         SetCRefCon(h, secs);            // tell control about it
  341.                         InvalRect(&(**h).contrlRect);    // force redraw
  342.                     }
  343.                 }
  344.             }
  345.         break;
  346.         
  347. //-----------------------------------------------------------------------------
  348. // show how a spinner control can adjust a progress bar control
  349. //-----------------------------------------------------------------------------
  350.  
  351.         case PROGBARS:
  352.             if(itemHit == SPIN10) {
  353.                 daSetCtlValue(theDialog, PROG9, 
  354.                     daGetCtlValue(theDialog, SPIN10));
  355.             }
  356.         break;
  357.         
  358. //-----------------------------------------------------------------------------
  359. // we don't do any special processing here for the other control examples,
  360. // but we do some special stuff for sliders via SetControlAction.
  361. //-----------------------------------------------------------------------------
  362.  
  363.         case POPUPS:
  364.         break;
  365.         case HSLIDERS:
  366.         break;
  367.         case VSLIDERS:
  368.         break;
  369.     }
  370. }
  371. //=============================================================================
  372. // A sample routine to show one method of saving the settings of the controls
  373. // on a panel. When a panel is "dismissed", the Dialog Manager disposes of the 
  374. // controls on the panel, so we have to save the values to restore if the panel
  375. // is displayed again.
  376. //
  377. // This is pretty "brute force" - just some global arrays of control values.
  378. //
  379. //=============================================================================
  380. static void savePanel(DialogPtr theDialog, short currPanel)
  381. {
  382.     short            inx;
  383.     
  384.     switch (currPanel) {
  385.         case BUTTONS:
  386.             for(inx=1;inx<=BUTTONCNT-2;inx++)
  387.                 gButtonValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  388.             gButtonValues[12] = daGetINum(theDialog, EDITSIZE);
  389.             gButtonValues[13] = daGetCtlValue(theDialog, FONTPOP);
  390.         break;
  391.         case POPUPS:
  392.             for(inx=1;inx<=POPUPCNT;inx++)
  393.                 gPopUpValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  394.         break;
  395.         case SPINNERS:
  396.             for(inx=1;inx<=SPINNERCNT;inx++)
  397.                 gSpinnerValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  398.         break;
  399.         case DATETIME:
  400.             for(inx=1;inx<=DATECNT;inx++)
  401.                 gDateValues[inx-1] = daGetCtlRefCon(theDialog, inx+STDCTLS);
  402.         break;
  403.         case HSLIDERS:
  404.             for(inx=1;inx<=HSLIDERCNT;inx++)
  405.                 gHSliderValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  406.         break;
  407.         case VSLIDERS:
  408.             for(inx=1;inx<=VSLIDERCNT;inx++)
  409.                 gVSliderValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  410.         break;
  411.         case PROGBARS:
  412.             for(inx=1;inx<=PROGBARCNT;inx++)
  413.                 gProgressValues[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  414.         break;
  415.         case POPUPEXT:
  416.             for(inx=1;inx<=POPEXTCNT;inx++)
  417.                 gPopExtensions[inx-1] = daGetCtlValue(theDialog, inx+STDCTLS);
  418.         break;
  419.     }
  420. }
  421.  
  422. //=============================================================================
  423. // A sample routine to show one method of restoring the control settings to
  424. // proper values when a panel is shown by clicking on its tab.
  425. //
  426. // This routine also does some special initialization of controls.
  427. //=============================================================================
  428. static void restorePanel(DialogPtr theDialog, short toPanel)
  429. {
  430.     ControlHandle    ch;
  431.     SpinHandle        hSpin;
  432.     short            inx;
  433.     
  434.     switch(toPanel) {
  435.     
  436.         case BUTTONS:
  437.             
  438. //-----------------------------------------------------------------------------
  439. // set font & restore control values
  440. //-----------------------------------------------------------------------------
  441.             
  442.             changeFont(theDialog, gFontNum, gFontSize);
  443.             for(inx=1;inx<=BUTTONCNT-2;inx++)
  444.                 daSetCtlValue(theDialog, inx+STDCTLS, gButtonValues[inx-1]);
  445.  
  446. //-----------------------------------------------------------------------------
  447. // set font controls
  448. //-----------------------------------------------------------------------------
  449.  
  450.             daSetINum(theDialog, EDITSIZE, gButtonValues[12]);
  451.             adjustSizePopup(theDialog, gButtonValues[12]);
  452.             daSetCtlValue(theDialog, FONTPOP, gButtonValues[13]);
  453.                 
  454. //-----------------------------------------------------------------------------
  455. // initialize the Tog buttons and disable one button
  456. //-----------------------------------------------------------------------------
  457.  
  458.             initTogButtons(theDialog, TB1,TB4);
  459.             daDimOne(theDialog, PB1, true);
  460.         break;
  461.         
  462.         case POPUPS:
  463.         
  464. //-----------------------------------------------------------------------------
  465. // set font & restore control values
  466. //-----------------------------------------------------------------------------
  467.  
  468.             changeFont(theDialog, geneva, 9);
  469.             for(inx=1;inx<=POPUPCNT;inx++)
  470.                 if(gPopUpValues[inx-1])
  471.                     daSetCtlValue(theDialog, inx+STDCTLS, gPopUpValues[inx-1]);
  472.         break;
  473.         
  474.         case SPINNERS:
  475.             
  476. //-----------------------------------------------------------------------------
  477. // set font & restore control values
  478. //-----------------------------------------------------------------------------
  479.             
  480.             changeFont(theDialog, 0, 0);        // since there is an edit field
  481.             
  482.             for(inx=1;inx<=SPINNERCNT;inx++)
  483.                 daSetCtlValue(theDialog, inx+STDCTLS, gSpinnerValues[inx-1]);
  484.             
  485. //-----------------------------------------------------------------------------
  486. // initialize edit text item to the value of the 3rd spinner
  487. //-----------------------------------------------------------------------------
  488.  
  489.             daSetINum(theDialog, EDITSPIN, 
  490.                         daGetCtlValue(theDialog,  SPIN3));
  491.             SelIText(theDialog, EDITSPIN, 32768, 32768);
  492.             
  493. //-----------------------------------------------------------------------------
  494. // the 6th spinner will adjust by 0.10, so stash a divisor value of 10
  495. // in the userData field
  496. //-----------------------------------------------------------------------------
  497.  
  498.             ch = daGetCtlHandle(theDialog, SPIN7);
  499.             if(ch) {
  500.                 hSpin = (SpinHandle)(**ch).contrlData;
  501.                 (**hSpin).userData = 10;
  502.                 fractSpin(ch, 1);                // draw the value
  503.             }
  504.             
  505.         break;
  506.         
  507.         case DATETIME:
  508.  
  509. //-----------------------------------------------------------------------------
  510. // set font & restore control values
  511. //-----------------------------------------------------------------------------
  512.  
  513.             changeFont(theDialog, geneva, 9);
  514.             for(inx=1;inx<=DATECNT;inx++)
  515.                 daSetCtlRefCon(theDialog, inx+STDCTLS, gDateValues[inx-1]);
  516.         break;    
  517.         
  518.         case HSLIDERS:
  519.  
  520. //-----------------------------------------------------------------------------
  521. // set font & restore control values
  522. //-----------------------------------------------------------------------------
  523.  
  524.             changeFont(theDialog, geneva, 9);
  525.             for(inx=1;inx<=HSLIDERCNT;inx++)
  526.                 daSetCtlValue(theDialog, inx+STDCTLS, gHSliderValues[inx-1]);
  527.                 
  528. //-----------------------------------------------------------------------------
  529. // set a ctlAction proc for slider 3
  530. //-----------------------------------------------------------------------------
  531.             
  532.             ch = daGetCtlHandle(theDialog, HSLIDER3);
  533.             SetCtlAction(ch, (ControlActionUPP)gTrackSlider);
  534.             daSetINum(theDialog, HSLIDERVAL3, GetCtlValue(ch));
  535.             
  536.             
  537.             ch = daGetCtlHandle(theDialog, HSLIDER9);
  538.             SetCtlAction(ch, (ControlActionUPP)gTrackSlider);
  539.             daSetINum(theDialog, HSLIDERVAL9, GetCtlValue(ch));
  540.         break;    
  541.         
  542.         case VSLIDERS:
  543.             changeFont(theDialog, geneva, 9);
  544.  
  545. //-----------------------------------------------------------------------------
  546. // restore control values
  547. //-----------------------------------------------------------------------------
  548.  
  549.             for(inx=1;inx<=VSLIDERCNT;inx++)
  550.                 daSetCtlValue(theDialog, inx+STDCTLS, gVSliderValues[inx-1]);
  551.                 
  552. //-----------------------------------------------------------------------------
  553. // set a ctlAction proc for slider 5
  554. //-----------------------------------------------------------------------------
  555.             
  556.             ch = daGetCtlHandle(theDialog, VSLIDER5);
  557.             SetCtlAction(ch, (ControlActionUPP)gTrackSlider);
  558.             daSetINum(theDialog, VSLIDERVAL5, GetCtlValue(ch));
  559.         break;    
  560.         
  561.         case PROGBARS:
  562.         
  563. //-----------------------------------------------------------------------------
  564. // set font & restore control values
  565. //-----------------------------------------------------------------------------
  566.  
  567.             changeFont(theDialog, geneva, 9);
  568.             for(inx=1;inx<=PROGBARCNT;inx++)
  569.                 daSetCtlValue(theDialog, inx+STDCTLS, gProgressValues[inx-1]);
  570.         break;    
  571.         case POPUPEXT:
  572.             changeFont(theDialog, 0, 0);
  573.             for(inx=1;inx<=POPEXTCNT;inx++)
  574.                 daSetCtlValue(theDialog, inx+STDCTLS, gPopExtensions[inx-1]);
  575.         break;
  576.     }
  577. }
  578. //=============================================================================
  579. //    a simple dialog filter proc
  580. //=============================================================================
  581. static  pascal char filter(DialogPtr theDialog, EventRecord *theEvent, short *theItem)
  582. {
  583.     char             result = FALSE;
  584.     char             c;
  585.     short            panelNum=0,inx,editItem,partCode;
  586.     long            min,max;
  587.     ControlHandle    cHdl;
  588.     Point            mousePt;
  589.     
  590.     panelNum = daGetCtlValue(theDialog, TABCNTL);
  591.         
  592.     switch(theEvent->what) {
  593.  
  594.         case nullEvent:
  595.         
  596. //-----------------------------------------------------------------------------
  597. // for this demo program, we'll use null events to demo the progress bar
  598. // cdefs on panel 7 of the dialog
  599. //-----------------------------------------------------------------------------
  600.  
  601.             if(panelNum == PROGBARS) {
  602.                 for(inx=STDCTLS+1;inx<=STDCTLS+8;inx++) {
  603.                     daSetCtlValue(theDialog, inx, 
  604.                                     daGetCtlValue(theDialog, inx)+1);
  605.                     if(daGetCtlValue(theDialog, inx) >= daGetCtlMax(theDialog, inx))
  606.                         daSetCtlValue(theDialog, inx, 0);
  607.                 }
  608.             }
  609.         break;
  610.         
  611.         case keyDown:
  612.          case autoKey:
  613.  
  614. //-----------------------------------------------------------------------------
  615. // for this demo, we'll show how to use command or control key 
  616. // combinations to operate the tabPanel CDEF
  617. //-----------------------------------------------------------------------------
  618.  
  619.              c = theEvent->message & charCodeMask;
  620.              
  621.             if(theEvent->modifiers & cmdKey) {
  622.                  result = panelCmdKey(theDialog, TABCNTL, c, theItem);
  623.                  if(result)
  624.                      break;
  625.              }
  626.             if(theEvent->modifiers & controlKey ||
  627.                 theEvent->modifiers & cmdKey) {
  628.                  result = panelCmdTab(theDialog, TABCNTL, c, theItem);
  629.                  if(result)
  630.                      break;
  631.              }
  632.  
  633. //-----------------------------------------------------------------------------
  634. // handle a forward delete key
  635. //-----------------------------------------------------------------------------
  636.                  
  637.              result = daForwardDel(theDialog, c);
  638.              if(result)
  639.                  break;
  640.              if(theEvent->modifiers & shiftKey) {
  641.                  result = daShiftSelect(theDialog, c);
  642.                  if(result)
  643.                      break;
  644.              }
  645.             editItem = ((DialogPeek)theDialog)->editField + 1;
  646.             
  647. //-----------------------------------------------------------------------------
  648. // limit to numeric entry only
  649. //-----------------------------------------------------------------------------
  650.  
  651.             if(panelNum == BUTTONS && editItem == EDITSIZE) {
  652.                 result = daEnterNumber(theDialog, editItem, 0L, 18L, c);
  653.             }
  654.             else 
  655.             if(panelNum == SPINNERS) {
  656.                 max = daGetCtlMax(theDialog, SPIN3);
  657.                 min = daGetCtlMin(theDialog, SPIN3);
  658.                 result = daEnterNumber(theDialog, editItem, min, max, c);
  659.             }
  660.          break;
  661.          
  662.          case mouseDown:
  663.             mousePt = theEvent->where;
  664.             GlobalToLocal(&mousePt);
  665.             partCode = FindControl(mousePt, theDialog, &cHdl);
  666.  
  667. //-----------------------------------------------------------------------------
  668.  // demonstrate use of FindControl/TrackControl within a dialog and to show
  669.  // how to continuously update the progress bar (item #10) from the value 
  670.  // of the spinner (item #9)
  671. //-----------------------------------------------------------------------------
  672.  
  673.              if(panelNum == PROGBARS) {
  674.                 if(partCode && cHdl == daGetCtlHandle(theDialog, SPIN10)) {
  675.                     TrackControl(cHdl, mousePt,  (ControlActionUPP)gTrackSpin);
  676.                 }
  677.              }
  678.              else
  679.  
  680. //-----------------------------------------------------------------------------
  681.  // spinner 7 is a control that adjusts by a fractional value
  682. //-----------------------------------------------------------------------------
  683.  
  684.              if(panelNum == SPINNERS) {
  685.                 if(partCode && cHdl == daGetCtlHandle(theDialog, SPIN7)) {
  686.                     TrackControl(cHdl, mousePt,  (ControlActionUPP)gFractSpin);
  687.                 }
  688.              }
  689.      
  690.          break;
  691.     }
  692.     return result;
  693. }
  694. //=============================================================================
  695. //    A simple action proc for call to TrackControl above.  Just sets the value
  696. //  of the progress bar to match the spinner.
  697. //=============================================================================
  698. static pascal void trackSpin(ControlHandle theControl, short partCode)
  699. {
  700.     DialogPtr theDialog = (**theControl).contrlOwner;
  701.     
  702.     if(partCode) {
  703.         daSetCtlValue(theDialog, PROG9, GetCtlValue(theControl));
  704.     }
  705. }
  706. //=============================================================================
  707. //    This routine is set as an actionProc via SetCtlAction to provide
  708. //  "live" display of the values of a Slider control.
  709. //    It simply displays the control value as a statText item (using daSetINum).
  710. //
  711. //    This one function is handling 3 different slider controls.
  712. //=============================================================================
  713. static pascal void trackSlider(ControlHandle theControl, short partCode)
  714. {
  715.     DialogPtr theDialog = (**theControl).contrlOwner;
  716.     
  717.     if(partCode) {
  718.         if(theControl == daGetCtlHandle(theDialog, VSLIDER5))
  719.             daSetINum(theDialog, VSLIDERVAL5, GetCtlValue(theControl));
  720.         else
  721.         if(theControl == daGetCtlHandle(theDialog, HSLIDER3))
  722.             daSetINum(theDialog, HSLIDERVAL3, GetCtlValue(theControl));
  723.         else
  724.         if(theControl == daGetCtlHandle(theDialog, HSLIDER9))
  725.             daSetINum(theDialog, HSLIDERVAL9, GetCtlValue(theControl));
  726.     }
  727. }
  728. //=============================================================================
  729. //    This Action proc shows how to have a spinner control with a fractional
  730. //  increment
  731. //=============================================================================
  732. static pascal void fractSpin(ControlHandle theControl, short partCode)
  733. {
  734.     DialogPtr        theDialog;
  735.     short            val,div;
  736.     ControlHandle    hCtrl;
  737.     SpinHandle        hSpin;
  738.     Str255            s;
  739.     double            dbl;
  740.     
  741.     if(!partCode)                    // avoid flicker
  742.         return;
  743.         
  744.     theDialog = (**theControl).contrlOwner;
  745.     
  746. //-----------------------------------------------------------------------------
  747. // grab the handle to the control, extract the userData value
  748. // to use as divisor to calculate the value to display in the text item
  749. //-----------------------------------------------------------------------------
  750.  
  751.     hCtrl = daGetCtlHandle(theDialog, SPIN7);
  752.     if(theControl == hCtrl) {
  753.         hSpin = (SpinHandle)(**theControl).contrlData;
  754.         div = (**hSpin).userData;
  755.         
  756.         val = GetCtlValue(hCtrl);
  757.         dbl = (double)val/div;    
  758.         sprintf((char *)s, "%3.1lf", dbl);
  759.         c2pstr((char *)s);
  760.         
  761. //-----------------------------------------------------------------------------
  762. // display the calcuated value (in this case, via a statText item)
  763. //-----------------------------------------------------------------------------
  764.  
  765.         daSetIText(theDialog, FRACTSPIN, s);
  766.     }
  767.         
  768. }
  769.  
  770. //=============================================================================
  771. //    A rather simple minded routine to change the background color of the
  772. //    dialog - to demonstrate how the controls look on various backgrounds.
  773. //=============================================================================
  774. static void changeBGColor    (DialogPtr theDialog)
  775. {
  776.     short            cInx,inx,max;
  777.     long            bg[]={-1, 0xEEEE, 0xDDDD, 0xCCCC, 0xBBBB, 0xAAAA};
  778. //    long            bg[]={-1, 0xAAAA, 0xCCCC, 0xDDDD, 0xEEEE};
  779.     AuxWinHandle    aux;
  780.     GrafPtr            savePort;
  781.     
  782.     GetPort(&savePort);
  783.     
  784.     cInx = daGetCtlValue(theDialog, BGCOLOR) -1;    // get menu setting
  785.     
  786.     GetAuxWin(theDialog, &aux);                        // current colors
  787.     if(aux) {
  788.         max = (**(**aux).awCTable).ctSize;            // look for content color
  789.         for(inx=0;inx<max;inx++) {                    // entry & change it.
  790.         
  791.             if((**(**aux).awCTable).ctSeed == wContentColor) {
  792.                 (**(**aux).awCTable).ctTable[inx].rgb.red     = bg[cInx];
  793.                 (**(**aux).awCTable).ctTable[inx].rgb.green = bg[cInx];
  794.                 (**(**aux).awCTable).ctTable[inx].rgb.blue    = bg[cInx];
  795.                 if(daGetCtlValue(theDialog, BGPAT) > 0) {
  796.                     daSetCtlValue(theDialog, BGPAT, 0);
  797.                     changeBGPat(theDialog, false);
  798.                 }
  799.                 SetWinColor(theDialog, (WCTabHandle)(**aux).awCTable);
  800.                 SetPort(theDialog);                    // do this or die..
  801.                 break;
  802.             }
  803.         }
  804.     }
  805.     SetPort(savePort);
  806. }
  807.  
  808. //=============================================================================
  809. //    Change the background pattern for the dialog
  810. //=============================================================================
  811. static void changeBGPat    (DialogPtr theDialog, Boolean redraw)
  812. {
  813.     short            cInx;
  814.     PixPatHandle    pixPat=nil;
  815.     GrafPtr            savePort;
  816.     
  817.     GetPort(&savePort);
  818.     SetPort(theDialog);
  819.     
  820.     cInx = daGetCtlValue(theDialog, BGPAT) -1;    // get menu setting
  821.     
  822.     if(cInx)
  823.         pixPat = GetPixPat(cInx+128);
  824.     
  825.     if(pixPat) {
  826.         BackPixPat(pixPat);
  827.     }
  828.     else {
  829.         BackPat(&qd.white);
  830.     }
  831.     if(redraw) {
  832.         InvalRect(&theDialog->portRect);
  833.     }
  834.     
  835.     SetPort(savePort);
  836. }
  837.  
  838. //=============================================================================
  839. // Get font & font size info from panel 1 controls and use to change font.
  840. //=============================================================================
  841. static void setNewFont (DialogPtr theDialog)
  842. {
  843.     ControlHandle        h=0;
  844.     short                t, val, newFont;
  845.     Rect                r;
  846.     long                newSize;
  847.     Str255                fontName, fontSize;
  848.     GrafPtr                thisPort;
  849.     popUpPrivateDataH    hPpd=0;
  850.  
  851. //-----------------------------------------------------------------------------
  852. // get the font size from the popup menu
  853. //-----------------------------------------------------------------------------
  854.  
  855.     GetDItem(theDialog,SIZEPOP,&t,(Handle *)&h,&r);
  856.     if(h) {
  857.         val = GetCtlValue(h);
  858.         hPpd = (popUpPrivateDataH)(*h)->contrlData;
  859.         if(hPpd) {
  860.             GetItem((*hPpd)->mHandle, val, fontSize);
  861.             StringToNum(fontSize, &newSize);
  862.             if(newSize == 12)
  863.                 newSize = 0;
  864.         }
  865.     }
  866.  
  867. //-----------------------------------------------------------------------------
  868. // get the font from the popup menu
  869. //-----------------------------------------------------------------------------
  870.     
  871.     GetDItem(theDialog,FONTPOP,&t,(Handle *)&h,&r);
  872.     if(h) {
  873.         val = GetCtlValue(h);
  874.         hPpd = (popUpPrivateDataH)(*h)->contrlData;
  875.         if(hPpd) {
  876.             GetItem((*hPpd)->mHandle, val, fontName);
  877.             GetFNum(fontName, &newFont);
  878.             
  879.             changeFont(theDialog, newFont, newSize);
  880.             
  881.             GetPort(&thisPort);
  882.             gFontNum = thisPort->txFont;
  883.             gFontSize = thisPort->txSize;
  884.         }
  885.     }
  886. //-----------------------------------------------------------------------------
  887. // redraw the dialog in the new font & size
  888. //-----------------------------------------------------------------------------
  889.  
  890.         DrawDialog(theDialog);
  891. }
  892.  
  893. //=============================================================================
  894. //    Set a new font for this dialog.  This is a nasty way to do this,
  895. //  but it appears to work.
  896. //=============================================================================
  897. static void changeFont(DialogPtr theDialog, short newFont, long newSize)
  898. {
  899.     FontInfo        fInfo;
  900.     
  901.     SetPort(theDialog);
  902.     
  903.     if(theDialog->txFont != newFont ||                // minimize flicker from
  904.         theDialog->txSize != newSize) {                // redrawing
  905.         
  906. //-----------------------------------------------------------------------------
  907. // set new font & size for current port
  908. //-----------------------------------------------------------------------------
  909.         
  910.         TextFont(newFont);
  911.         TextSize((short)newSize);
  912.         
  913. //-----------------------------------------------------------------------------
  914. // set new font for TEHandle in dialog (this is a crude hack!)
  915. //-----------------------------------------------------------------------------
  916.         
  917.         GetFontInfo(&fInfo);
  918.         (*((DialogPeek)theDialog)->textH)->txFont = newFont;
  919.         (*((DialogPeek)theDialog)->textH)->txSize = newSize;
  920.         (*((DialogPeek)theDialog)->textH)->fontAscent = fInfo.ascent;
  921.         (*((DialogPeek)theDialog)->textH)->lineHeight = 
  922.                             fInfo.ascent+fInfo.descent+fInfo.leading;
  923.     }
  924. }
  925.  
  926. //=============================================================================
  927. //    Set a new size into the popup menu for font size.  Called when the 
  928. //    EDITSIZE dialog edit item is changed, will add or remove a "custom" size
  929. //    and a separator line as required.
  930. //=============================================================================
  931. static void    adjustSizePopup    (DialogPtr theDialog, long newSize)
  932. {
  933.     short                type,val,inx,max;
  934.     long                longVal;
  935.     ControlHandle        h;
  936.     MenuHandle            hMenu;
  937.     Rect                r;
  938.     popUpPrivateDataH    hPpd;
  939.     Str255                s;
  940.     Boolean                found=false;
  941.     
  942.     GetDItem(theDialog,SIZEPOP,&type,(Handle *)&h,&r);    // get menu handle
  943.     hPpd = (popUpPrivateDataH)(*h)->contrlData;
  944.     hMenu = (*hPpd)->mHandle;
  945.     
  946.     val = GetCtlValue(h);                                // uncheck current item
  947.     CheckItem(hMenu, val, FALSE);
  948.     
  949.     max = CountMItems(hMenu);                            // number of menu items
  950.     
  951.     for(inx=1;inx<=max;inx++) {                            // see if typed in value
  952.         GetItem(hMenu, inx, s);                            // matches a menu item
  953.         StringToNum(s, &longVal);
  954.         if(longVal == newSize) {                        // yep, got a match but it is
  955.             if(inx > 2 & max > MAXFONTSIZES) {            // not custom, so let's
  956.                 DelMenuItem(hMenu,1);                    // delete the custom setting
  957.                 DelMenuItem(hMenu,1);                    // and the separator line
  958.                 inx-=2;
  959.             }
  960.             CheckItem(hMenu, inx, TRUE);                // check new item
  961.             SetCtlValue(h, inx);
  962.             found = true;
  963.             break;
  964.         }
  965.     }
  966.     if(!found) {                                        // no match, set custom item
  967.         NumToString(newSize, s);
  968.         if(max == MAXFONTSIZES) {                        // add a custom value
  969.             InsMenuItem(hMenu, "\p(-", 0);                // first, a separator
  970.             InsMenuItem(hMenu, s, 0);                    // now, the new size
  971.         }
  972.         else {
  973.             SetItem(hMenu, 1, s);                        // replace existing custom
  974.         }
  975.         CheckItem(hMenu, 1, TRUE);
  976.         SetCtlValue(h, 1);
  977.     }
  978. }
  979.  
  980. //=============================================================================
  981. // find the System font item in the popup menu created with AddResMenu
  982. //=============================================================================
  983. static short findSystemFont(DialogPtr theDialog)
  984. {
  985.     Str255                fontName;
  986.     ControlHandle        hCtl;
  987.     MenuHandle            hMenu;
  988.     short                t,max,inx,newFont=2;
  989.     Rect                r;
  990.     popUpPrivateDataH    hPpd;
  991.     
  992.     GetDItem(theDialog, FONTPOP, &t, (Handle *)&hCtl, &r);
  993.     if(hCtl) {
  994.         hPpd = (popUpPrivateDataH)(*hCtl)->contrlData;
  995.         hMenu = (*hPpd)->mHandle;
  996.         if(hMenu) {
  997.             max = CountMItems(hMenu);
  998.         
  999.             for(inx=1;inx<=max;inx++) {
  1000.                 GetItem(hMenu, inx, fontName);
  1001.                 GetFNum(fontName, &newFont);
  1002.                 if(newFont == systemFont)
  1003.                     break;
  1004.                     
  1005.             }
  1006.         }
  1007.     }
  1008.     return(inx);
  1009. }
  1010.  
  1011. //=============================================================================
  1012. // a simple side-by-side comparison between CDEF 63 (System 7) & CDEF 101
  1013. //=============================================================================
  1014. extern void comparePopup()
  1015. {
  1016.     DialogPtr        theDialog;
  1017.     GrafPtr            savePort;
  1018.     short            itemHit,inx,t;
  1019.     ControlHandle    hCtl,hCtl2;
  1020.     Rect            r;
  1021.     
  1022.     theDialog = GetNewDialog(259,0L,(DialogPtr)-1);
  1023.     if(theDialog) {
  1024.         GetPort(&savePort);
  1025.         SetPort(theDialog);
  1026.         TextFont(monaco);
  1027.         TextSize(9);
  1028.         
  1029.         ShowWindow(theDialog);
  1030.         
  1031.         do {
  1032.             movableModalDialog ((ModalFilterUPP)nil,&itemHit);
  1033.             if(itemHit == 3) {        // disable button
  1034.                 for(inx = 4;inx<=15;inx++) {
  1035.                     GetDItem(theDialog, inx, &t, (Handle *)&hCtl, &r);
  1036.                         if((*hCtl)->contrlHilite == 0xff)
  1037.                             HiliteControl(hCtl,0);
  1038.                         else
  1039.                             HiliteControl(hCtl,255);
  1040.                 }
  1041.                 GetDItem(theDialog, 3, &t,(Handle *)&hCtl2, &r);
  1042.                 if((*hCtl)->contrlHilite == 0xff)
  1043.                     SetCTitle(hCtl2, "\pEnable");
  1044.                 else
  1045.                     SetCTitle(hCtl2, "\pDisable");
  1046.             }        
  1047.         }while(itemHit != ok && itemHit != cancel);
  1048.         DisposDialog(theDialog);
  1049.         SetPort(savePort);
  1050.     }
  1051. }